package cn.javadog.smartbreeding.core.config.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yuyong
 * @date: 2018-11-26 3:50 PM
 * desc: copy的jfinal的，只是加了一个ok
 */
public class Ret extends HashMap {

    private static final String STATE = "status";
    private static final String STATE_OK = "ok";
    private static final String STATE_FAIL = "fail";

    private static final String DEFAULT_KEY = "data";
    private static final String FAIL_KEY = "errmsg";

    public Ret() {
    }

    public static Ret ok() {
        return new Ret().setOk();
    }

    public static Ret ok(Object key, Object value) {
        return ok().set(key, value);
    }

    public static Ret ok(Object value) {
        return ok().set(DEFAULT_KEY, value);
    }

    public static Ret fail() {
        return new Ret().setFail();
    }

    public static Ret fail(Object value) {
        return fail().set(FAIL_KEY, value);
    }

    public static Ret fail(Object key, Object value) {
        return fail().set(key, value);
    }

    public Ret setOk() {
        super.put(STATE, STATE_OK);
        return this;
    }

    public Ret setFail() {
        super.put(STATE, STATE_FAIL);
        return this;
    }

    public boolean isOk() {
        Object state = get(STATE);
        if (STATE_OK.equals(state)) {
            return true;
        }
        if (STATE_FAIL.equals(state)) {
            return false;
        }

        throw new IllegalStateException("调用 isOk() 之前，必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
    }

    public boolean isFail() {
        Object state = get(STATE);
        if (STATE_FAIL.equals(state)) {
            return true;
        }
        if (STATE_OK.equals(state)) {
            return false;
        }

        throw new IllegalStateException("调用 isFail() 之前，必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
    }

    public Ret set(Object key, Object value) {
        super.put(key, value);
        return this;
    }

    public Ret set(Map map) {
        super.putAll(map);
        return this;
    }

    public Ret set(Ret ret) {
        super.putAll(ret);
        return this;
    }

}


